﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Work.Hardware;

namespace FreesenseSubAIS.Core
{
    public class CameraViework : ICameraAdapter
    {
        private VieworksCamera _camera;
        private double _exporse;
        public string CameraName { get; private set; }

        public bool CameraState { get; private set; }

        public CameraViework(string name)
        {
            CameraName = name;
        }
        public bool SetExprosetime(double extime)
        {
            if (!CameraState || !(extime > 14) || !(extime < 7000000)) return false;
            _camera.SetExposureTime(extime);
            IsLargeExprose(extime);
            return true;
        }
        private void IsLargeExprose(double extime)
        {
            if (_exporse != 0 && _exporse > 10000 && _exporse > extime && _exporse / extime > 5)
                Thread.Sleep(200);
            _exporse = extime;
        }
        public bool OneShot()
        {
            bool flag = false;
            if (CameraState)
            {
                _camera.OneShot();
                flag = true;
            }
            return flag;
        }

        public bool ContinueShot()
        {
            bool flag = false;
            if (CameraState)
            {
                _camera.StartContinuouShot();
                flag = true;
            }
            return flag;
        }

        public bool StopShot()
        {
            bool flag = false;
            if (CameraState)
            {
                _camera.StopContinuouShot();
                flag = true;
            }
            return flag;
        }

        public bool Close()
        {
            bool flag = false;
            if (CameraState)
            {
                //_camera.Close();
                flag = true;
            }
            return flag;
        }

        public double GetExprosetime()
        {
            double extime = 0;
            if (CameraState)
            {
                extime = _camera.GetExposureTime();
            }
            return extime;
        }

        public double GetErrorCount()
        {
            double errorcount = 0;
            return errorcount;
        }

        public event EventHandler<Bitmap> ImageReady;

        public async Task Init()
        {
            await Task.Run(() =>
           {
               _camera = new VieworksCamera("1", "COM5");
               //this.c.NewMsgPushing += new Camera.CamMsgCallback(this.c_NewMsgPushing);
               _camera.FrameReady += _camera_FrameReady;
               //Console.WriteLine(_camera.CamInfo.ToString());
               _camera.ConnectCamera();
               CameraState = true;
           });
        }
        private void _camera_FrameReady(Bitmap bitmap)
        {
            if (ImageReady != null)
                ImageReady(CameraName, bitmap);
        }
        public bool AquireBitmap()
        {
            return OneShot();
        }

        public bool AquireBitmap(string mapname)
        {
            return OneShot();
        }

        public bool AquireBitmap(int mapindex)
        {
            return OneShot();
        }
    }
}
